package com.CustomClasses;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

public class InsertMySQL extends FetchMySQL {
	private  List<HashMap<String, String>> columnsValues;
	private static final String url_insert = "http://www.worldcupcompendium.com/android_connect/create_product.php";
	
	private static final String url_update = "http://www.worldcupcompendium.com/android_connect/update_product.php";
	
	private static boolean isRunningOnBackGround = false;
	
	JSONParser jsonParser = new JSONParser();
	
	public InsertMySQL(String tableName, List<String> columnsName,
			String userId, List<HashMap<String, String>> list) {
		super(tableName, columnsName, userId);
		this.columnsValues = list;
	}

	
	@Override
	protected List<HashMap<String, String>> doInBackground(String... args) {
	
			if(!isRunningOnBackGround){
			isRunningOnBackGround = true;
			List<HashMap<String, String>> mapListFromSelect = super.doInBackground();
			
			List<HashMap<String, String>> valuesToInsert = new ArrayList<HashMap<String,String>>();
			
			List<HashMap<String, String>> valuesToUpdate = new ArrayList<HashMap<String,String>>();
				
				for(HashMap<String, String> mapReq: columnsValues){
					if(!mapListFromSelect.contains(mapReq)){
						boolean toUpdate = false;
						for(HashMap<String, String> mapSel: mapListFromSelect){
							if(mapReq.get(columnsName.get(0)).equals(mapSel.get(columnsName.get(0)))){
								toUpdate =true;
							}
						}
						if(toUpdate){
							valuesToUpdate.add(mapReq);
						} else {
							valuesToInsert.add(mapReq);
						}
						
					}
				}
				 
			for(HashMap<String, String> mapReq : valuesToInsert){
				StringBuilder sBuilder = new StringBuilder();
				List<NameValuePair> paramsToInsert = new ArrayList<NameValuePair>();
				paramsToInsert.addAll(params);
				int cont = 0;
				for(String column: columnsName){
				sBuilder.append(mapReq.get(column));
				cont++;
					if(cont<columnsName.size()){
					sBuilder.append(";");
					}
				}
				paramsToInsert.add(new BasicNameValuePair("columnsValues", sBuilder.toString()));
				
				jsonParser.makeHttpRequest(url_insert,
						"POST", paramsToInsert);
				
			}
			
			for(HashMap<String, String> mapReq : valuesToUpdate){
				StringBuilder sBuilder = new StringBuilder();
				List<NameValuePair> paramsToInsert = new ArrayList<NameValuePair>();
				paramsToInsert.addAll(params);
				int cont = 0;
				for(String column: columnsName){
				sBuilder.append(mapReq.get(column));
				cont++;
					if(cont<columnsName.size()){
					sBuilder.append(";");
					}
				}
				paramsToInsert.add(new BasicNameValuePair("columnsValues", sBuilder.toString()));
				
				jsonParser.makeHttpRequest(url_update,
						"POST", paramsToInsert);
			}
			List<HashMap<String, String>> allUpdatedeValues = new ArrayList<HashMap<String,String>>();
			allUpdatedeValues.addAll(valuesToUpdate);
			allUpdatedeValues.addAll(valuesToInsert);
			isRunningOnBackGround = false;
			return allUpdatedeValues;
			} else {
				return null;
			}
				
	}

}
